/****************************************************************************
 * Copyright (C) 2012 Dj_Skual
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ****************************************************************************/
#include "ListGCInfos.h"
#include "../../GC/gctools.h"
#include "../../Themes/CTheme.h"
#include "../../DeviceControls/DeviceHandler.hpp"

GCInfosWindow::GCInfosWindow(Browser * b, int w, int h)
	: GuiFrame(w, h)
{
	browser = b;
	browser->IndexChanged.connect(this, &GCInfosWindow::OnBrowserIndexChange);
	
	selIndex = -1;
	
	fileGC = Resources::GetImageData("icon_brows_gc.png");
	iconImg = NULL;
	
	type_Gamecube = Resources::GetImageData("icon_type_gamecube.png");
	typeImg = new GuiImage(type_Gamecube);
	typeImg->SetAlignment(ALIGN_RIGHT | ALIGN_TOP);
	
	nameTxt = new GuiText((char*)NULL, thInt("16 - mainbrowser browser infos text size"), thColor("r=0 g=0 b=0 a=255 - mainbrowser entry text color"));
	nameTxt->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	nameTxt->SetMaxWidth(width, SCROLL_HORIZONTAL);
	
	subnameTxt = new GuiText((char*)NULL, thInt("16 - mainbrowser browser infos text size"), thColor("r=0 g=0 b=0 a=255 - mainbrowser entry text color"));
	subnameTxt->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	subnameTxt->SetMaxWidth(width, DOTTED);
	
	idTxt = new GuiText((char*)NULL, thInt("16 - mainbrowser browser infos text size"), thColor("r=0 g=0 b=0 a=255 - mainbrowser entry text color"));
	idTxt->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	idTxt->SetMaxWidth(width, DOTTED);
	
	permTxt = new GuiText((char*) NULL, thInt("16 - mainbrowser browser infos text size"), thColor("r=0 g=0 b=0 a=255 - mainbrowser entry text color"));
	permTxt->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	permTxt->SetMaxWidth(width, DOTTED);
	
	sizeTxt = new GuiText((char*)NULL, thInt("16 - mainbrowser browser infos text size"), thColor("r=0 g=0 b=0 a=255 - mainbrowser entry text color"));
	sizeTxt->SetAlignment(ALIGN_LEFT | ALIGN_TOP);
	sizeTxt->SetMaxWidth(width, DOTTED);
	
	path = new GuiText(tr( "Path:" ), thInt("16 - mainbrowser browser infos text size"), thColor("r=0 g=0 b=0 a=255 - mainbrowser entry text color"));
	path->SetAlignment(ALIGN_LEFT | ALIGN_BOTTOM);
	path->SetPosition(0, -51);
	
	pathTxt = new GuiText((char*)NULL, thInt("16 - mainbrowser browser infos text size"), thColor("r=0 g=0 b=0 a=255 - mainbrowser entry text color"));
	pathTxt->SetLinesToDraw(3);
	pathTxt->SetInterline(3);
	pathTxt->SetMaxWidth(width, WRAP);
	pathTxt->SetAlignment(ALIGN_LEFT | ALIGN_BOTTOM);
	pathTxt->SetPosition(0, -32);
}

GCInfosWindow::~GCInfosWindow()
{
	RemoveAll();
	
	if(iconImg)
		delete iconImg;
	delete typeImg;
	delete nameTxt;
	delete subnameTxt;
	delete idTxt;
	delete permTxt;
	delete sizeTxt;
	delete path;
	delete pathTxt;
	
	Resources::Remove(fileGC);
	Resources::Remove(type_Gamecube);
}

void GCInfosWindow::OnBrowserIndexChange()
{
	SetInfos();
}

void GCInfosWindow::SetInfos()
{
	RemoveAll();
	
	if(iconImg)
		delete iconImg;
	iconImg = NULL;
	
	if(!browser->GetEntrieCount())
		return;
	
	selIndex = browser->GetSelIndex();
	
	GetCardIcon();
	
	int y = MAX(iconImg->GetHeight(), typeImg->GetHeight()) + 7;
	nameTxt->SetText(browser->GetGCName(selIndex));
	nameTxt->SetPosition(0, y);
	y += 19;
	
	subnameTxt->SetText(browser->GetGCSubname(selIndex));
	subnameTxt->SetPosition(0, y);
	y += 25;
	
	std::string ID = tr("ID:");
	ID += " ";
	ID += browser->GetGCID(selIndex);
	idTxt->SetText(ID);
	idTxt->SetPosition(0, y);
	y += 19;
	
	std::string perm = tr( "Perm:" );
	perm += " ";
	perm += GetStrPerm(browser->GetGCPermissions(selIndex));
	permTxt->SetText(perm);
	permTxt->SetPosition(0, y);
	y += 19;
	
	std::string size = tr("Size:");
	size += fmt(" %0.f %s", browser->GetGCBlocks(selIndex), tr( "Block(s)" ));
	sizeTxt->SetText(size);
	sizeTxt->SetPosition(0, y);
	
	std::string cardPath = browser->GetCurrentPath();
	cardPath += fmt(":/%s", browser->GetGCFilename(selIndex));
	pathTxt->SetText(cardPath);
	
	Append(iconImg);
	Append(typeImg);
	Append(nameTxt);
	Append(subnameTxt);
	Append(idTxt);
	Append(permTxt);
	Append(sizeTxt);
	Append(path);
	Append(pathTxt);
}

void GCInfosWindow::GetCardIcon()
{
	MemCardHandle * Card = NULL;
	if(!browser->GetCurrentPath().compare("gca"))
		Card = DeviceHandler::Instance()->GetGCAHandle();
	else
		Card = DeviceHandler::Instance()->GetGCBHandle();
	
	if(Card)
		iconImg = Card->GetIcon(browser->GetGCFilename(selIndex), 0x30, 0x30);
	
	if(!iconImg)
		iconImg = new GuiImage(fileGC);
}
